Page({
  data: {
    searchValue: '',
    categories: [
      { id: 'all', name: '全部', icon: '🔥' },
      { id: 'scenery', name: '美景', icon: '🌄' },
      { id: 'food', name: '美食', icon: '🍽️' },
      { id: 'culture', name: '文化', icon: '🏦' },
      { id: 'life', name: '生活', icon: '🏠' },
      { id: 'travel', name: '旅行', icon: '✈️' }
    ],
    selectedCategory: 'all',
    postsList: [
      {
        id: 1,
        title: '七彩丹霞的早晨，美得像仙境',
        content: '今早在山顶看日出，云彩万千，太震撒了！',
        category: 'scenery',
        categoryName: '美景',
        author: '小丸子',
        avatar: 'cloud://cloud1-5gn2jipsaa21f49d.636c-cloud1-5gn2jipsaa21f49d-1381622514/avatar-g.png',
        time: '2分钟前',
        likes: 23,
        comments: 8,
        images: ['cloud://cloud1-5gn2jipsaa21f49d.636c-cloud1-5gn2jipsaa21f49d-1381622514/1607399662510095485.jpg'],
        isLiked: false
      },
      {
        id: 2,
        title: '这家火锅店真的太好吃了！',
        content: '底料鲜美，菜品新鲜，价格实惠，强烈推荐！',
        category: 'food',
        categoryName: '美食',
        author: '美食家',
        avatar: 'cloud://cloud1-5gn2jipsaa21f49d.636c-cloud1-5gn2jipsaa21f49d-1381622514/avatar-g.png',
        time: '5分钟前',
        likes: 45,
        comments: 12,
        images: ['cloud://cloud1-5gn2jipsaa21f49d.636c-cloud1-5gn2jipsaa21f49d-1381622514/九宫格火锅.jpg'],
        isLiked: true
      },
      {
        id: 3,
        title: '古镇的春天，每一个角落都是风景',
        content: '漫步在青石板路上，感受历史的沉淀和春天的温暖。',
        category: 'scenery',
        categoryName: '美景',
        author: '旅行者',
        avatar: '../../images/avatar.png',
        time: '10分钟前',
        likes: 67,
        comments: 15,
        images: ['../../images/avatar.png'],
        isLiked: false
      },
      {
        id: 4,
        title: '传统文化节盛大开幕',
        content: '精彩的民间表演，传统手工艺展示，让人大开眼界！',
        category: 'culture',
        categoryName: '文化',
        author: '文化爱好者',
        avatar: '../../images/avatar.png',
        time: '15分钟前',
        likes: 89,
        comments: 22,
        images: ['../../images/avatar.png'],
        isLiked: true
      },
      {
        id: 5,
        title: '家常小菜分享：糖醋里脊',
        content: '酸甜可口，老少皆宜，做法简单，快来学学！',
        category: 'food',
        categoryName: '美食',
        author: '家常厨房',
        avatar: '../../images/avatar.png',
        time: '20分钟前',
        likes: 34,
        comments: 9,
        images: ['../../images/avatar.png'],
        isLiked: false
      }
    ]
  },

  onLoad() {
    console.log('讨论页面加载');
  },

  onShow() {
    // 同步自定义tabbar状态
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      });
    }
  },

  // 搜索输入
  onSearchInput(e) {
    this.setData({
      searchValue: e.detail.value
    });
  },

  // 搜索
  onSearch() {
    if (!this.data.searchValue.trim()) {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'none'
      });
      return;
    }
    wx.showToast({
      title: `搜索: ${this.data.searchValue}`,
      icon: 'none'
    });
  },

  // 选择分类
  onCategoryTap(e) {
    const categoryId = e.currentTarget.dataset.id;
    this.setData({
      selectedCategory: categoryId
    });
  },

  // 帖子点击
  onPostTap(e) {
    const postId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/postDetail/index?id=${postId}`,
      fail: () => {
        wx.showToast({
          title: '帖子详情页开发中',
          icon: 'none'
        });
      }
    });
  },

  // 点赞
  onLikeTap(e) {
    const postId = e.currentTarget.dataset.id;
    const postsList = this.data.postsList.map(post => {
      if (post.id === postId) {
        post.isLiked = !post.isLiked;
        post.likes += post.isLiked ? 1 : -1;
      }
      return post;
    });
    
    this.setData({ postsList });
    
    wx.showToast({
      title: postsList.find(p => p.id === postId).isLiked ? '已点赞' : '取消点赞',
      icon: 'none'
    });
  },

  // 评论
  onCommentTap(e) {
    const postId = e.currentTarget.dataset.id;
    wx.showToast({
      title: '评论功能开发中',
      icon: 'none'
    });
  },

  // 发帖
  onCreatePost() {
    wx.navigateTo({
      url: '/pages/createPost/index',
      fail: () => {
        wx.showToast({
          title: '发帖功能开发中',
          icon: 'none'
        });
      }
    });
  },

  // 图片加载错误处理
  onImageError(e) {
    console.log('图片加载失败:', e.detail.errMsg);
  },

  onImageLoad(e) {
    console.log('图片加载成功');
  },

  onAvatarError(e) {
    console.log('头像加载失败:', e.detail.errMsg);
  },

  // 获取过滤后的帖子列表
  getFilteredPosts() {
    const { postsList, selectedCategory } = this.data;
    if (selectedCategory === 'all') {
      return postsList;
    }
    return postsList.filter(post => post.category === selectedCategory);
  },

  // 跳转到发布页面
  goToEdit() {
    wx.navigateTo({
      url: '/pages/edit/index'
    });
  }
});